﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using ReAnimation.DAL.RepositoryInterfaces;
using ReAnimation.Domain;
using ReAnimation.MVP.Presenters;
using ReAnimation.MVP.ViewInterfaces;
using ReAnimation.Helpers.Common;

namespace ReAnimation.MVP.Views
{
    public partial class ZMatrixTable : BaseUserControlView, IZMatrixTable
    {
        
        private bool _errorState;
        private readonly ZMatrixTablePresenter _presenter;

        public ZMatrix ZMatrix
        {
            get
            {
                return GrabZMatrixFromTheTable();
            }
            set
            {
                LoadZMatrixIntoTable(value);
            }
        }

        public bool ErrorState
        {
            get { return _errorState; }
            set
            {
                _errorState = value;
                if(_errorState)
                {
                    errorStateLabel.Text = "Error ZMatrix";
                    errorStateLabel.ForeColor = Color.Red;
                }
                else
                {
                    errorStateLabel.Text = "ZMatrix is ok";
                    errorStateLabel.ForeColor = Color.Green;
                }

            }
        }

        public event EventHandler ZMatrixChanged;
       

        public ZMatrixTable(IAtomDescriptionRepository atomDescriptionRepository,
                            ZMatrixTablePresenter presenter)
        {
            InitializeComponent();
            _presenter = presenter;

            IEnumerable<AtomDescription> descriptions = atomDescriptionRepository.GetAll().ToArray();

            atomsDescriptionsBindingSource.DataSource =
                from d in descriptions select new { DisplayMember = d.Name, ValueMember = d};

            Atom.DisplayMember = "DisplayMember";//FIXME: ugly
            Atom.ValueMember = "ValueMember";

            _presenter.View = this;
        }

        private ZMatrix GrabZMatrixFromTheTable()
        {
            return new ZMatrix{ZMatrixLines = dataGridView.DataSource as IList<ZMatrixLine>};
        }

        private void LoadZMatrixIntoTable(ZMatrix zMatrix)
        {
            if (zMatrix == null)
                zMatrixBindingSource.DataSource = null;
            else
            {
                
                zMatrixBindingSource.DataSource = zMatrix.ZMatrixLines;
            }
        }

        private void addRowButton_Click(object sender, EventArgs e)
        {
            zMatrixBindingSource.AddNew(); 
        }

        private void removeRowButton_Click(object sender, EventArgs e)
        {
            if(zMatrixBindingSource.Count !=1)//TODO: is it right place
             zMatrixBindingSource.RemoveCurrent();
        }

        private void bindingSource_CurrentChanged(object sender, EventArgs e)
        {
          //  ZMatrixChanged.Throw(this,EventArgs.Empty);
        }

        private void dataGridView_CellErrorTextNeeded(object sender, DataGridViewCellErrorTextNeededEventArgs e)
        {
            e.ErrorText = null;
        }

        private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.ThrowException = false;
        }

        private void dataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            Debug.WriteLine(zMatrixBindingSource.Count);
        }

        private void dataGridView_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            ZMatrixChanged.Throw(this, EventArgs.Empty);
        }

        private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            
            ZMatrixChanged.Throw(this, EventArgs.Empty);
            

        }
        //TODO: finish with it
        private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
          /*var comboBox = e.Control as ComboBox;
            if(comboBox != null)
            {
                Debug.WriteLine(comboBox.Handle);
                
                comboBox.SelectedIndexChanged -= AtomDescriptionComboBoxSelectedIndexChanged;
                comboBox.SelectedIndexChanged += AtomDescriptionComboBoxSelectedIndexChanged;
            }*/
        }

        private void AtomDescriptionComboBoxSelectedIndexChanged(object sender, EventArgs eventArgs)
        {
           /* var comboBox = sender as ComboBox;
            var zMatrixLine = (zMatrixBindingSource.DataSource as IList<ZMatrixLine>)[dataGridView.SelectedCells[0].RowIndex];
            zMatrixLine.AtomDescription = comboBox.SelectedValue as AtomDescription;
            ZMatrixChanged.Throw(this, EventArgs.Empty);*/
        }

        private void dataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
           // dataGridView.Rows[e.RowIndex].HeaderCell.Value = (e.RowIndex + 1).ToString();
        }

        private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            String numberString = (e.RowIndex + 1).ToString();
            Font font = DefaultFont;
            var stringSize = e.Graphics.MeasureString(numberString, font);
            var drawPosition = new Point
                                   {
                                       X = (int) (dataGridView.RowHeadersWidth/2 - stringSize.Width/2),
                                       Y =
                                           (int)
                                           (e.RowBounds.Y + dataGridView.Rows[e.RowIndex].Height/2 - stringSize.Height/2)
                                   };

            e.Graphics.DrawString(numberString,font, 
                Brushes.Black,
               drawPosition.X,
               drawPosition.Y);
        }
    }
}
